<template>
  <div id="answerPopup">
    <van-popup :close-on-click-overlay="false" :get-container="'answerPopup'" v-model="show" @close="close" round :style="{ background: 'transparent' }">
      <div class="box">
        <div class="box_top">
          <div class="answer_popup_title">参与答题</div>
          <div class="answer_popup_title">获取{{integral}}奖励</div>
          <img :src="imgHost" alt="" class="pos" />
        </div>
        <div class="box_bottom">
          <div class="box_bottom_title">{{ infoData.content }}</div>
          <div class="opt">
            <div @click="tapSelect(item.id)" :class="[checkIds(item.id) ? 'opt_lis active_d' : 'opt_lis active_q']" v-for="(item, index) in infoData.opt" :key="index">
              {{ item.content }}
            </div>
          </div>
          <div class="btn" @click="submitHandel">提交</div>
        </div>
      </div>
      <div class="iconBox">
        <i class="iconfont icon-adsystem_icon_cancle" @click="close"></i>
      </div>
    </van-popup>
  </div>
</template>
<script>
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    infoData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      answer: "",
      imgHost: `${window.location.origin}/static/local/live-broadcast-question/question.png`,
      integral: window.localStorage.integral,
    };
  },
  mounted() {},
  watch: {
    show(val) {
      if (val) {
        this.answer = "";
      }
    }
  },
  computed: {
    checkIds(id) {
      return function(id) {
        if (this.infoData.select_type == 1) {
          if (this.answer.indexOf(id) != -1) {
            return true;
          }
        } else {
          return this.answer == id;
        }
      };
    }
  },
  methods: {
    tapSelect(id) {
      if (this.infoData.select_type == 1) {
        // 多选
        if (this.answer.length == 0) {
          this.answer = [];
          this.answer.push(id);
        } else {
          if (this.answer.indexOf(id) == -1) {
            this.answer.push(id);
          } else {
            this.answer.splice(this.answer.indexOf(id), 1);
          }
        }
      } else {
        // 单选
        this.answer = id;
      }
      console.log(this.answer);
    },
    close() {
      this.$emit("update:show", false);
    },
    async submitHandel() {
      let _json = {
        room_id: this.fun.getKey("room_id"),
        question_id: this.infoData.id,
        answer_ids: this.infoData.select_type == 1 ? this.answer : [this.answer]
      };
      let { data, msg, result } = await $http.post("plugin.live-broadcast-question.frontend.controllers.index.submit-answer", _json, "");
      if (result) {
        this.$emit("submit", data);
      } else {
        this.$toast(msg);
      }
      // 提交按钮
    }
  }
};
</script>

<style lang="scss" scoped>
#answerPopup {
  ::-webkit-scrollbar {
    width: 5px !important; /* 对垂直流动条有效 */
  }
  ::-webkit-scrollbar-thumb {
    background: #f15353; // 滑块颜色（与主题色一致）
    border-radius: 2px;
    &:hover {
      background: darken(#f15353, 1%); // 悬停时加深颜色
    }
  }
  scrollbar-color: #f15353 #f5f5f5; // 滑块颜色 轨道颜色
  scrollbar-width: 5px;
}
.iconBox {
  margin: 0 auto;
  margin-top: 1.56rem;
  .icon-adsystem_icon_cancle {
    font-size: 2rem;
    color: #ffffff;
  }
}
.box {
  background: #f15353;
  width: 18.75rem;
  border-radius: 0.75rem;
  margin: 0 auto;
  .box_top {
    padding: 0.78rem 0.94rem;
    box-sizing: border-box;
    text-align: left;
    position: relative;
    .answer_popup_title {
      font-weight: bold;
      font-size: 1.13rem;
      color: #ffffff;
    }
    .pos {
      position: absolute;
      right: 0.5rem;
      bottom: -0.5rem;
      width: 4.34rem;
      height: 4.22rem;
    }
  }
  .box_bottom {
    position: relative;
    width: 18.75rem;
    background: #fff;
    border-radius: 0.75rem;
    padding: 0.94rem;
    box-sizing: border-box;
    .box_bottom_title {
      text-align: left;
      font-weight: 500;
      font-size: 0.94rem;
      color: #00001c;
      margin-bottom: 0.47rem;
    }
    .opt {
      margin-bottom: 1.81rem;
      max-height: 16rem;
      overflow-y: scroll;

      .opt_lis {
        background: #f5f5f5;
        border-radius: 0.5rem;
        font-size: 0.88rem;
        padding: 0.81rem 0.63rem;
        box-sizing: border-box;
        text-align: left;
        margin-bottom: 0.75rem;
      }
      .active_d {
        background: #feeded;
        border: 0.03rem solid #f15353;
        color: #f15353;
      }
      .active_q {
        border: 0.03rem solid #fff;
      }
      .opt_lis:last-child {
        margin-bottom: 0;
      }
    }
    .btn {
      height: 2.5rem;
      background: #f15353;
      border-radius: 1.25rem;
      font-size: 0.94rem;
      color: #ffffff;
      text-align: center;
      line-height: 2.5rem;
    }
  }
}
</style>
